var App = require('app');

App.AppSessionView = Em.View.extend({
    templateName: 'templates/app/session',
    didInsertElement: function() {
    	var me = this;
    	
    	AppUtil.conectToSession(Vari.CurrSessionId, function(msg){
    		FormUtil.clearForm();
    		Vari.frm = FormUtil.retriveInfo(msg.data.formId);
    		me.displayForm(msg);
    	});
    },
    displayForm: function(msg) {
    	var i = 1;
		Vari.frm.fields.forEach(function(field) {
			Ember.mixin(field, {
				indexx: i++
			});
			var type = field.type;
			var utilName = Util.capFirstLetter(type) + "Util";
			eval(utilName + ".view(field)");
		});
		
		$("#formId").val(Vari.frm.get("id"));
		$("#headline").text(Vari.frm.get("headline"));
		
		$("#submit").width($("#form_canvas_view").width() - 10 + "px");
		
		$("#form_canvas_view").on("change", ".fieldset.multipleChoice input:radio", function(ev){
			FormUtil.changeVal($(ev.target));
		});
		
		if (msg.data.type == "form") {
			$("form#displayForm").show();
		} else {
			$("form#displayForm").hide();
		}
    }
});